// GTY


#include "ceshi/TPAuraGameplayAbility.h"

#include "Character/AuraCharacter.h"

void UTPAuraGameplayAbility::ActivateAbility(
	const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo,
	const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData
)
{
	Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);
}

void UTPAuraGameplayAbility::AuraDisplacement(FVector MouseLocation ,float Distance) 
{
	AAuraCharacter* AuraCharacter = Cast<AAuraCharacter>(GetAvatarActorFromActorInfo());
	
	FTransform SpawnTransform;//生成的位置
	SpawnTransform.SetLocation(AuraCharacter->GetActorLocation());
	SpawnTransform.SetRotation(AuraCharacter->GetActorRotation().Quaternion());
	AAuraTPtexiao* Projectile = GetWorld()->SpawnActorDeferred<AAuraTPtexiao>//延迟生成
(
	TPtexiaoClass,//指定生成的演员类。
	SpawnTransform,//指定生成位置和旋转。
	GetOwningActorFromActorInfo(),//指定生成的演员的拥有者。
	Cast<APawn>(GetOwningActorFromActorInfo()),//指定生成的演员的归属者。
	ESpawnActorCollisionHandlingMethod::AlwaysSpawn//指定生成的演员的碰撞处理方式。始终生成。
);
	Projectile->FinishSpawning(SpawnTransform);//完成生成

	FVector StartPoint = AuraCharacter->GetActorLocation(); // 起点
	FVector EndPoint = MouseLocation;   // 终点

	// 计算方向向量
	FVector DirectionVector = EndPoint - StartPoint;

	// 如果需要单位向量（标准化方向）
	FVector NormalizedDirection = DirectionVector.GetSafeNormal();
	FVector ResultLocation = StartPoint + NormalizedDirection * Distance;

	FRotator NewRotation = DirectionVector.Rotation();
	AuraCharacter->SetActorRotation(NewRotation);
	// 确保 Z 轴不低于地面
	ResultLocation.Z = FMath::Max(ResultLocation.Z, 1.f);
	// 设置角色的新位置
	AuraCharacter->SetActorLocation(ResultLocation);
}
